//common.js
var page=0;
var height=0;
var fx=0;//方向0左 1右
var ved=1;//視頻
var status1=0;
var status2=0;
var status3=0;
var newHeight=0;
var special=0;//特殊情況
function printYMcontent(ym){
//		alert(ym)
	//ym:2017-05
	ym=ym.substring(0,4)+'-'+ym.substring(5,7);
//	alert(ym);
//	alert('11');
	var param={}
	param.ym=ym;
//	param.userId=$('#userId').val();
//	param.bookType=$('#bookType').val();
	url=WST.U('WebApp/WbBooks/printYMcontent');
//	alert(url)
	$.post(url,param,function(data){
		page=0;
		height=0;
		fx=0;//方向0左 1右
		ved=1;//視頻
		var json = data;
		var str='';
		var con='';
		var dayLogo='';
		//console.log(JSON.stringify(json))
		//清空页面
		$('.main_container').empty();
		if(json.status=='1'){
//			alert('11')
			page++;
			//先打印插页
			if(json.inset=='1'){
			str+='<div class="main" data-page="'+page+'"><div class="chaye"><div class="chaye"><div class="mainImg">';
			$.each(data.insert,function(key,val){
				if(json.ym.substr(5)==val.month){
					str+='<img src="'+WST.ROOT +'/'+val.img+'" class="img"></div>';
				}
			})

			str+='<div class="yearTime">'+json.ym.substring(-1,4)+'</div>';
			str+='</div></div></div>';
			$('.main_container').append(str);
			page++;
			}
			//生成一页
			addOnePage(ym,json.shade)
//			alert(json.content.length)

			//组装页面
			//main=753
			//日期=40
			//字一行=18

			for(var i=0;i<json.content.length;i++){
				if(json.content[i].text==null && json.content[i].msg=='' && json.content[i].vedio==''){
					continue;
				}
				var main=json.content[i];
				//日期
				if(main.d!=''){
					height+=23;
					//情况一：日期下有文字，2张图片以上，不加圖片，如有视频 则加视频(在左排)
					if(main.text!=null && main.msg.length>1 && fx==0){
						status1=9*Number(main.line)+6;
						if(main.vedio!=''){
							status3=145;
						}
						newHeight=status1+status3;
					}
					//情况二：日期下只有一张图片
					else if(main.msg.length==1 && fx==0){
						if(main.text==null){
							status1=10*Number(main.line)+6;
						}
						if(main.msg[0]!=0){
							status2=Number(main.msg[0]);
						}
						newHeight=status1+status2;
					}
					//情况三：日期下只有视频
					else if(main.vedio!='' && fx==0){
						newHeight=145;
					}
					//情况四：在右排
					else if(main.text!=null && fx==1){
						status1=10*Number(main.line)+6;
						if(main.msg!='' && main.msg[0]!=0){
							status2=Number(main.msg[0]);
						}
						if(main.vedio!=''){
							status3=145;
						}
						newHeight=status1+status2+status3;
					}else if(main.text==null && main.msg!='' && main.msg[0]!=0 && fx==1){
						newHeight=Number(main.msg[0]);
					}
					if(Number(height+newHeight)<406-9){
						dayLogo+='<div class="Main_week"><div class="week_left left"><div class="day">'+main.d+'</div><div class="week_time">星期'+main.w+'</div>';
						dayLogo+='<img src=" '+WST.ROOT +'/Apps/WebApp/View/default/images/edit_date_icon.png" style="width:1.06rem;height:100%;"></div>';
						dayLogo+='<div class="week_right left">'+main.s+'</div></div><input type="hidden" value="'+main.date+'">';
						$('.page_'+fx+'_'+page).append(dayLogo);
						con='';
					}
					else{
						height=0;
						if(fx==1){
							fx=0;
							page++;
							addOnePage(ym,json.shade)
							dayLogo+='<div class="Main_week"><div class="week_left left"><div class="day">'+main.d+'</div><div class="week_time">星期'+main.w+'</div>';
							dayLogo+='<img src=" '+WST.ROOT +'/Apps/WebApp/View/default/images/edit_date_icon.png" style="width:1.06rem;height:100%;"></div>';
							dayLogo+='<div class="week_right left">'+main.s+'</div></div><input type="hidden" value="'+main.date+'">';
							$('.page_0_'+page).append(dayLogo);
							con='';
							height+=24;
						}else{
							fx=1;
							//如果是去到右排打印日期的话 并且接下来的内容有图片且只能放下一张图片，加顶部距离
							if(main.msg!=''){
								special+=(24+Number(main.msg[0]))
								if(main.text!=null){
									special+=(10*Number(main.line)+6);
								}
								if(main.msg.length==1){
									if(special+91<406-9){
										con+='<div style="width: 100%;height:91px"></div>';
										height+=91;
									}
								}else if(main.msg.length>1){
									special+=(Number(main.msg[1]));
									if(special>=406-9 && special-Number(main.msg[1])+160<397){
										con+='<div style="width: 100%;height:91px"></div>';
										height+=91;
									}
								}
							}
							$('.page_1_'+page).append(con);
							dayLogo+='<div class="Main_week"><div class="week_left left"><div class="day">'+main.d+'</div><div class="week_time">星期'+main.w+'</div>';
							dayLogo+='<img src=" '+WST.ROOT +'/Apps/WebApp/View/default/images/edit_date_icon.png"  style="width:1.06rem;height:100%;"></div>';
							dayLogo+='<div class="week_right left">'+main.s+'</div></div><input type="hidden" value="'+main.date+'">';
							$('.page_1_'+page).append(dayLogo);
							con='';
							height+=23;
						}
					}
					status1=0;
					status2=0;
					status3=0;
					newHeight=0;
					special=0;
				}
				//文字
				if(main.text!=null){
					height+=10*Number(main.line)+6;
					if(height<406-9){
						con+='<div class="Main_text">'+main.text+'</div>';
						$('.page_'+fx+'_'+page).append(con);
						con='';
					}
					else{
						height=0;
						if(fx==1){
							fx=0;
							page++;
							addOnePage(ym,json.shade);
							con+='<div class="Main_text">'+main.text+'</div>';
							$('.page_0_'+page).append(con);
							con='';
							height+=10*Number(main.line)+6;
						}else{
							fx=1;
							con+='<div class="Main_text">'+main.text+'</div>';
							$('.page_1_'+page).append(con);
							con='';
							height+=10*Number(main.line)+6;
						}
					}
				}
				//照片
				if(main.msg!=''){
					for(var k=0;k<main.msg.length;k++){
						if(main.msg[k]!=0){
							if(main.imgs[k].toString().indexOf('load')>0){
								imgUrl='http://youyiku.sxnet.cc/'+main.imgs[k]
							}else{
								imgUrl=main.imgs[k]
							}
//								alert(main.msg[k])
							var hh=Number(main.msg[k])
							height+=hh+8;
							if(height<406-10){
								con+='<div style="height:'+hh+'px;width:100%;margin:5px 0 3px;"><img src="'+imgUrl+'" style="height: '+hh+';width:100%"></div>';
								$('.page_'+fx+'_'+page).append(con);
								con='';
							}
							else{
								height=0;
								if(fx==1){
									fx=0;
									page++;
									addOnePage(main.ym,json.shade);
									$('.page_0_'+page).append(dayLogo);
									height+=24;
									con+='<div style="height: '+hh+'px;width:100%;margin:5px 0 3px;"><img src="'+imgUrl+'" style="height: '+hh+';width:100%"></div>';
									$('.page_0_'+page).append(con);
									con='';
									height+=hh+8;
								}else{
									fx=1;
									//如果在右排打印完这张图片后 后面还有图片，并且放不下，就加顶部距离
									if(k!=main.msg.length-1){
										if(hh+Number(main.msg[k+1])>406-9 && hh+106<406-9){
											con+='<div style="width: 100%;height:106px"></div>';
											height+=106;
										}
									}
									//或者是最后一张，并后面日期的内容放不下，就加顶部距离
									else if(k==main.msg.length-1 && i!=json.content.length-1){
										special+=10+24;
										if(json.content[i+1].text!=null){
											special+=10*Number(json.content[i+1].line)+6;
										}
										if(json.content[i+1].msg!=''){
											special+=Number(json.content[i+1].msg[0]);
										}
										if(special+hh>406-9 && hh+114<406-9){
											con+='<div style="width: 100%;height:114px"></div>';
											height+=114;
										}
									}
									con+='<div style="height: '+hh+'px;width:100%;margin-bottom:2px;"><img src="'+imgUrl+'" style="height: '+hh+';width:100%"></div>';
									$('.page_1_'+page).append(con);
									con='';
									height+=hh+2;
								}
							}
						}


					}
				}
				//視頻
				if(main.vedio!=''){
					height+=145;
					if(height<406-9){
						con+='<div class="Main_text erwei'+ved+'"><div class="imgLogo"></div></div>';
						$('.page_'+fx+'_'+page).append(con);
						con='';
						createErweima(main.vedio)
					}else{
						height=0;
						if(fx==1){
							fx=0;
							page++;
							addOnePage(main.ym,json.shade);
							con+='<div class="Main_text erwei'+ved+'"><div class="imgLogo"></div></div>';
							$('.page_'+fx+'_'+page).append(con);
							con='';
							height+=145;
							createErweima(main.vedio)
						}else{
							fx=1;
							con+='<div class="Main_text erwei'+ved+'"><div class="imgLogo"></div></div>';
							$('.page_'+fx+'_'+page).append(con);
							con='';
							height+=145;
							createErweima(main.vedio)
						}
					}
				}
				//结束一个数据
				if(i!=json.content.length-1){
					height+=10;
					if(height<406-131){
						con+='<div style="height:10px;"></div>'
						$('.page_'+fx+'_'+page).append(con);
						con='';
					}else{
//						height=0;
//						if(fx==1){
//							fx=0;
//							page++;
//							addOnePage(ym)
//						}else{
//							fx=1
//						}
					}
				}
				dayLogo='';
			}
//				alert(con)
			var yellow='';
//			alert(typeof json.next.substr(5,6));

//alert('TEXT:'+newYm);
			if(json.next){
				var newYm=json.next.substr(0,4).toString()+'-'+json.next.substr(5,6).toString();
				yellow+='<div class="yellow" onclick="printYMcontent(\''+newYm+'\')">';
				yellow+='<div class="lookTime">';
				yellow+='<div class="look_time" >查看'+json.next.substring(-1,4)+'年'+json.next.substring(5)+'月</div>'
				yellow+='<div class="down"><img class="img" src="'+WST.ROOT+'/Apps/WebApp/View/default/images/edit_to_view_icon.png"></div>';
				yellow+='</div>';
				yellow+='</div>';
			}
			else{

				yellow+='<div class="yellow">';
				yellow+='<div class="lookTime">';
				yellow+='<div class="look_time" onclick="backcover()">已经是最后一个月啦</div>';
				yellow+='</div></div>';


			}

			//alert(json.next);
			$('.main_container').append(yellow);
			yellow='';
		}
		else{

		}
		$('body').scrollTop(0);
	});

}

function addOnePage(ym,shade){
	var str='';
	var mod = page%2;
	console.log(mod);
	if(shade==1){
		str+='<div class="Main" data-page="'+page+'"><div class="Main_main">'
	}else if(shade==2){
		if(mod==1){
			str+='<div class="Main" data-page="'+page+'"><div class="Main_main2">';
		}else{
			str+='<div class="Main" data-page="'+page+'"><div class="Main_main3">';
		}
	}
	else if(shade==3){
		if(mod==1){
			str+='<div class="Main" data-page="'+page+'"><div class="Main_main4">';
		}else{
			str+='<div class="Main" data-page="'+page+'"><div class="Main_main5">';
		}

	}
	else{
		str+='<div class="Main" data-page="'+page+'"><div class="Main_main6">'
	}

	str+='<div class="Main_center">';
	str+='<div class="Main_left page_0_'+page+'"></div>';//左框
	str+='<div class="Main_right page_1_'+page+'"></div>';
	str+='</div></div></div>';
	$('.main_container').append(str);
}
//需要qrjs2.min.js
function createErweima(url){
//		alert(url)
	var container1 = $('.erwei'+ved);
	var svgElement = document.createElement("div"),

		u = url,
		s = QRCode.generateSVG(u, {
			ecclevel: "M",
			fillcolor: "#FFFFFF",
			textcolor: "#373737",
//						margin: 4,
//						modulesize: 4
		});
	svgElement.appendChild(s);
	container1.append(svgElement);
	ved++;
}